package com.example.dollarShipping.controller.driver;


import com.example.dollarShipping.entity.Menu;
import com.example.dollarShipping.entity.TruckOrder;
import com.example.dollarShipping.security.UserDetailsImpl;
import com.example.dollarShipping.service.DriverService;
import com.example.dollarShipping.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/driver")
public class DriverController {

    @Resource
    DriverService driverService;

    @Resource
    private MenuService menuService;

    @ModelAttribute("menuList")
    public List<Menu> backGround() {
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        List<Menu>  menu= menuService.getMenu(userDetails.getRoleId());
        return menu;
    }

    @RequestMapping
    public String showPage(Model model) {
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        List<TruckOrder> tl = driverService.getTruckOrder(userDetails.getId());
        model.addAttribute("userId",userDetails.getId());
        model.addAttribute("driverTruckOrders",tl);
        return "driver/driverTruckOrder";
    }

    // 货运单签收/结算操作
    // 修改货运单状态并添加任务记录
    @PostMapping("/driverOperate")
    public String driverOperate(@RequestParam Map<String,Object> params){
        System.out.println("driverOperate");
        Integer state = Integer.parseInt(params.get("state").toString());
        Long id = Long.parseLong(params.get("id").toString());
        Long operator = Long.parseLong(params.get("operator").toString());
        String location ="";
        String unexpectedInfo ="";
        Double unexpectedPrice = 0.0;
        if(params.get("location")!=null)
              location  = (String) params.get("location");
        if(params.get("unexpectedInfo")!=null)
            unexpectedInfo = (String) params.get("unexpectedInfo");
        if(params.get("unexpectedPrice")!=null)
            unexpectedPrice = Double.parseDouble(params.get("unexpectedPrice").toString());
        try {
            driverService.changeState(state, id, operator, location,unexpectedInfo,unexpectedPrice);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "driver/driverTruckOrder";
    }




}
